<?php
namespace app\client\controller;
use app\common\lib\Redis;

class Store extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function goods() {
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $lastPkId = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 10;
        $goodsName = isset($params['goodsName']) ? trim($params['goodsName']) : '';
        $userId = isset($params['storeId']) ? intval($params['storeId']) : $this->userid;
        $categoryId = isset($params['category']) ? intval($params['category']) : 0;
        
        $where = [['a.user_id', '=', $userId], ['a.chat_type', 'in', [2, 4, 5]]];
        if($lastPkId > 0){
            $where[] = ['a.chat_id', '<', $lastPkId];
        }
        
        if($goodsName !== ''){
            $where[] = ['goods_name', 'like', "%{$goodsName}%"];
        }
        
        if($categoryId > 0) {
            $where[] = ['b.category_id', '=', $categoryId];
        }
        
        $offset = ($page - 1) * $pageSize;
        $list = model('Chat')->alias('a')->join('__CHAT_GOODS__ b', 'a.chat_id=b.chat_id')->where($where)->field('a.cover, a.chat_type, a.chat_id, b.goods_name, b.sale_price, b.agent_price')->order('a.chat_id desc')->limit(0, $pageSize)->select()->toArray();
        return successResult(['list' => $list]);
        
    }
    
    public function library() {
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $lastPkId = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 10;
        $goodsName = isset($params['goodsName']) ? trim($params['goodsName']) : '';
        $userId = isset($params['storeId']) ? intval($params['storeId']) : $this->userid;
        
        $where = [['user_id', '=', $userId]];
        if($lastPkId > 0){
            $where[] = ['id', '<', $lastPkId];
        }
        
        if($goodsName !== ''){
            $where[] = ['goods_name', 'like', "%{$goodsName}%"];
        }
        
        
        $offset = ($page - 1) * $pageSize;
        $list = model('GoodsStore')->where($where)->field(['id', 'cover', 'goods_name', 'sale_price', 'agent_price'])->order('id desc')->limit(0, $pageSize)->select()->toArray();
        
        return successResult(['list' => $list]);
    }
    
    public function libraryDelete() {
        $id = request()->post('id', 0, 'intval');
        if($id <= 0) {
            return errorResult();
        }
        
        $model = model('GoodsStore');
        
        $goods = $model->findOne(['id' => $id], ['user_id']);
        if(empty($goods) || $goods['user_id'] != $this->userid) {
            return errorResult();
        }
        
        $rs = $model->where(['id' => $id])->delete();
        if(!$rs) {
            return errorResult();
        }
        
        return successResult();
    }
    
    
    public function qrCode() {
        $storeId = request()->get('storeId', $this->userid, 'intval');
        $scene = '?to=store&storeId=' . $storeId;
        
        $redis = new Redis();
        $ACCESS_TOKEN = $redis->get('wx_access_token');
        $redis->close();
        if(empty($ACCESS_TOKEN)) {
            return errorResult();
        }
        
        $ch = curl_init('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $ACCESS_TOKEN);
        curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['scene' => $scene, 'width' => 280, 'is_hyaline' => true]));//POST数据
        $result = curl_exec($ch);
        if(empty($result)) {
            return errorResult();
        }
        if(stripos($result, '{') === 0) {
            return errorResult();
        }
        
        return $result;
    }
}










